package o2o.web.actions.order;

import com.jinyou.utils.common.AjaxResult;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.order.OrderSubmitCalResult;
import o2o.dao.PO.user.UserInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

//提交订单管理类
@Controller
public class SubmitOrderCalController {

    @RequestMapping(method = RequestMethod.POST, value = "/user/order/submit/pre")
    @ResponseBody
    public static String calculateOrderMoney(String token,
                                             String sysAppKey,
                                             Long shopId,
                                             Long deliveryId,
                                             Integer isUrgent,
                                             String goodsInfoJson,
                                             Integer isZiQu,
                                             Double buyLat, Double buyLng,
                                             Double taxMoney
    ) {
        //验证用户有效性
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        OrderSubmitCalResult result = SubmitOrderCal.calculateOrderMoney(
                sysAppKey,
                user.getUsername(), shopId,
                deliveryId,
                isUrgent,
                goodsInfoJson,
                isZiQu,
                buyLat, buyLng,
                taxMoney
        );
        if (null != result && result.isSuccess()) {
            return AjaxResult.successResult(result);
        } else {
            return AjaxResult.errorResult(result.getError());
        }
    }


}
